/**
 * let auto = new Audio(); // 音乐播对象
    auto.src = 'http://1.mp3';
    auto.load(); //加载音乐

    auto.play(); //播放
    auto.pause(); //暂停
 */

function playMusic() {
    // 实例化音乐对象
    let auto = new Audio();

    // 初始化当前正在播放的音乐序号
    let currentIndex = 0;
    // 初始化播放状态, false暂停true播放
    let play = false;

    // 事件代理方式，将播放点击事件交给ul父元素处理
    let ulEle = document.getElementsByTagName('ul')[0];
    ulEle.onclick = function () {
        let e = event || window.event;
        let target = e.target || e.srcElement;
        if (target.nodeName == 'IMG') {
            // 获取播放列表序号index
            let liEle = target.parentElement.parentElement.parentElement;
            let index = liEle.id;
            //获取音乐资源
            let url = liEle.getAttribute('url');
            //初始化音乐
            auto.src = url;
            auto.load();//加载音乐

            if (index != currentIndex) {
                //暂停上一个正在播放音乐项，
                if (currentIndex != 0) {
                    let oldLiEle = document.getElementById(currentIndex);
                    let imgEle = oldLiEle.lastElementChild.lastElementChild.firstElementChild;
                    imgEle.src = './image/pause.png';
                }
                //设置当前播放项为播放状态
                target.src = './image/play.png';
                play = true;
                currentIndex = index;

                auto.play();//音乐播放
            } else {
                if (play) {
                    play = false;
                    target.src = './image/pause.png';

                    auto.pause();//音乐暂停
                } else {
                    play = true;
                    target.src = './image/play.png';

                    auto.play();//音乐播放
                }
            }
        }
    }
}

playMusic();